package com.atguigu.szdz.dcsyl.common.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.szdz.dcsyl.entity.YlDataEntity;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.List;

@Component
public class YlListener extends AnalysisEventListener<YlDataEntity> {


    //存储读取的数据
    private List<YlDataEntity> list = new ArrayList<YlDataEntity>();

    /**
     * 每一行读取的时候调用
     *
     * @param ylDataEntity
     * @param analysisContext
     */
    @Override
    public void invoke(YlDataEntity ylDataEntity, AnalysisContext analysisContext) {
        // 打印读取到的数据
        System.out.println(ylDataEntity);
        try {
            list.add(ylDataEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取完成后调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("Excel读取完成,共" + list.size() + "条数据");
    }

    //获取读取到的数据
    public List<YlDataEntity> getList() {
        return list;
    }
}
